package com.yay.warehouse;

import java.util.Date;

import com.yay.warehouse.model.Category;
import com.yay.warehouse.model.LFType;
import com.yay.warehouse.service.L10N;
import com.yay.warehouse.service.LocationService;
import com.yay.warehouse.service.TicketsService;
import com.yay.warehouse.service.LocationService.LocationResult;
import com.yay.warehouse.tabs.TicketsAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Step1 extends Activity implements OnClickListener {
	private LocationService locationService;
    private TextView description;
    private TextView lblName;
    private TextView lblCategory;
    private TextView lblDescription;
    private TextView lblPhone;
    private EditText edtPhone;
    private EditText edtName;
    private EditText edtDescription;
    private Button btnDone; 
    private Spinner categories;
    private Location location;
    private Intent step2;
	
    
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        L10N.init(this.getResources());
		setContentView(R.layout.step1);
		if(location == null) {
			getLocationService().getLocation(this, locationResult);
		}
		
	    // init controls
		getDescription();
	    getLblName();
	    getLblCategory();
		getLblDescription();
		getBtnDone(this);
		getLblPhone();
		getEdtPhone().setText(getContactPhone());
	    
		switch(LFType.findById(TicketsService.getCurrentTicket().getType())) {
		     case LOST: getDescription().setText(this.getResources().getString(R.string.s1_ldescription)); break;
		     default: 	getDescription().setText(this.getResources().getString(R.string.s1_fdescription));
	    }
	     
	    initCategories();
	}
	
	@Override
   	public void onClick(View arg0) {
   		if(getBtnDone(this).equals(arg0)) { 
			if(getEdtName().getText() == null || "".equals(getEdtName().getText().toString().trim())) {
				AlertDialog.Builder alertbox = new AlertDialog.Builder(Step1.this);
		        alertbox.setMessage(getDescription().getText());
		        alertbox.setNeutralButton(Step1.this.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
		        	public void onClick(DialogInterface arg0, int arg1) {
		            }
		        });
		        alertbox.show();
	   		 } else if(getEdtPhone().getText() == null || "".equals(getEdtPhone().getText().toString().trim())) {
					AlertDialog.Builder alertbox = new AlertDialog.Builder(Step1.this);
			        alertbox.setMessage(this.getResources().getString(R.string.s1_warnPhone));
			        alertbox.setNeutralButton(Step1.this.getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
			        	public void onClick(DialogInterface arg0, int arg1) {
			            }
			        });
			        alertbox.show();
		     } else {
	 			TicketsService.getCurrentTicket().setDate(IKeys.SDF.format(new Date()));
	    		TicketsService.getCurrentTicket().setContactPhone(getEdtPhone().getText().toString());
	    		TicketsService.getCurrentTicket().setName(getEdtName().getText().toString());
	    		TicketsService.getCurrentTicket().setDescription(getEdtDescription().getText().toString());
	    		if(L10N.debug) {
	    			TicketsService.getCurrentTicket().setLatitude(48.463602 * 1E6);
					TicketsService.getCurrentTicket().setLongitude(35.054584 * 1E6);
	    		}
	    		TicketsAdapter.reload = true;
	    		startActivity(getStep2());
	    		finish();
	   		 }
   		}
   	}
   	
	@Override
	public void onResume() {
		super.onResume();
		Dashboard.reload = true;
	}


	// PRIVATE METHODS
	
    private LocationService getLocationService() {
		if(locationService == null) {
			locationService = new LocationService();
		}
		return locationService;
	}
    
    private TextView getDescription() {
    	if(description == null) {
    		description = (TextView)findViewById(R.id.description);
    		description.setTypeface(L10N.font);
    	}
    	return description;
    }
    
    private TextView getLblName() {
    	if(lblName == null) {
    		lblName = (TextView)findViewById(R.id.lblName);
    		lblName.setTypeface(L10N.font);
    	}
    	return lblName;
    }
    
    private TextView getLblCategory() {
    	if(lblCategory == null) {
    		lblCategory = (TextView)findViewById(R.id.lblCategory);
    		lblCategory.setTypeface(L10N.font);
    	}
    	return lblCategory;
    }
    
    private TextView getLblDescription() {
    	if(lblDescription == null) {
    		lblDescription = (TextView)findViewById(R.id.lblDescription);
    		lblDescription.setTypeface(L10N.font);
    	}
    	return lblDescription;
    }
    
    private TextView getLblPhone() {
    	if(lblPhone == null) {
    		lblPhone = (TextView)findViewById(R.id.lblPhone);
    		lblPhone.setTypeface(L10N.font);
    	}
    	return lblDescription;
    }

    private EditText getEdtPhone() {
    	if(edtPhone == null) {
    		edtPhone = (EditText)findViewById(R.id.edtPhone);
    	}
    	return edtPhone;
    }
    
    private EditText getEdtName() {
    	if(edtName == null) {
    		edtName = (EditText)findViewById(R.id.edtName);
    	}
    	return edtName;
    }
    
    private EditText getEdtDescription() {
    	if(edtDescription == null) {
    		edtDescription = (EditText)findViewById(R.id.edtDescription);
    	}
    	return edtDescription;
    }

    private Button getBtnDone(OnClickListener listener) {
    	if(btnDone == null) {
    	    btnDone = (Button) findViewById(R.id.btnDone);
    	    btnDone.setTypeface(L10N.font);
            btnDone.setOnClickListener(listener);
    	}
    	return btnDone;
    }
    
    private void initCategories() {
    	if(categories == null) {
    	    categories = (Spinner) findViewById(R.id.spCategories);
    	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
    	    for(Category cat : Category.values()) {
    	    	adapter.add(this.getResources().getString(cat.getName()));
    		}
    	    categories.setAdapter(adapter);
    	    categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    	    	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    	    		 TicketsService.getCurrentTicket().setCategory(pos);
    	        }
    	        public void onNothingSelected(AdapterView<?> parent) {
    	        }
    	    });
    	    categories.setSelection(0);
    	}
    }

	private String getContactPhone(){
		String ret = null;
        try {
        	TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
        	ret = telMgr.getLine1Number();
        } catch(Exception e) {
        	Log.e(this.getClass().getName(), e.toString());
        }
        return ret;
	}
	 
	private LocationResult locationResult = new LocationResult() {
		@Override
	    public void gotLocation(final Location location){
			Step1.this.location = location;
			if(location != null) {
				TicketsService.getCurrentTicket().setLatitude(location.getLatitude() * 1E6);
				TicketsService.getCurrentTicket().setLongitude(location.getLongitude() * 1E6);
			}
	    }
	};
	
	private Intent getStep2() {
		if(step2 == null) {
			step2 = new Intent(Step1.this, Step2.class); 
		}
		return step2;
	}
		 
}
